package tas.report;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import tas.dao.DaoRegimenLaboral;
import tas.dao.impl.DaoRegimenLaboralImpl;

@WebServlet(name = "ServletConsultaRegLaboralExc", urlPatterns = {"/view/admin/ConsultaRegLaboralExc"})
public class ServletConsultaRegLaboralExc extends HttpServlet {

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        String autorizacion = (String) request.getSession().getAttribute("autorizacion");
        String ID = (String) request.getSession().getAttribute("ID");

        if ((ID == null) || (autorizacion == null)
                || (!autorizacion.equals("ADMIN"))
                || (!ID.equals(request.getSession().getId()))) {

            response.sendRedirect("../");

        } else {

            request.setCharacterEncoding("UTF-8");
            String fpath = request.getServletContext().getRealPath("") + "/excel/consultaRegimenLaboral.xlsx";

            try {
                InputStream is = new FileInputStream(fpath);
                Workbook wb = WorkbookFactory.create(is);
                Sheet sheet = wb.getSheetAt(0);

                //String data = request.getParameter("data_excel");
                // nuevo (apoyado de ServletConsultas_Dep)
                
//String reglab = request.getParameter("reglab_x");
       
                DaoRegimenLaboral daoRegimenLaboral = new DaoRegimenLaboralImpl();
                 String filtro = " (1=1) ";
                 
//                String filtro = " (SEDE.IDAÑO = '" + annio + "' AND SEDE.IDUNIDADEJECUTORA = '" + uejecutora + "' AND SEDE.IDCORTE = '" + corte + "') ";
                               
                List<Object[]> list = daoRegimenLaboral.RegimenLaboralQRYExc(filtro);

                StringBuilder sb = new StringBuilder();
                int cont = 1;
                // armando data (construccion de tabla del ServletConsultas_Dep, aqui se tranforma en trama)
                for (Object[] f : list) {
                    sb.append(cont).append(";");
                    sb.append(f[0]).append(";");
                    sb.append(f[1]).append(";");
                    sb.append(f[2]).append(";");
                    cont++;
                }
                String data = sb.toString();
                data = data.substring(0, data.length() - 1);
                // --- fin nuevo
                
                String[] dato = data.split(";");
                int fil = 2;
                int col = 0;
                Row row = null;
                for (String d : dato) {
                    if ((col % 4) == 0) {
                        row = sheet.createRow(2 + fil);
                        col = 0;
                        ++fil;
                    }

                    if (row != null) {
                        row.createCell(col + 1).setCellValue(d);
                    }

                    ++col;
                }

                response.setContentType(
                        "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");

                OutputStream out = response.getOutputStream();
                wb.write(out);
                out.close();

            } catch (InvalidFormatException ex) {
            }
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
